package com.yunmang.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yunmang.R;
import com.yunmang.activity.SearchEndActivity;
import com.yunmang.adapter.PiciLeftAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.TransportBean;
import com.yunmang.entity.event.EndEvent;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/26.
 */
public class PiciFragment3 extends BaseFragment {
    @InjectView(R.id.next_station)
    TextView nextStation;
    @InjectView(R.id.et_hdbh_search)
    EditText etHdbhSearch;
    @InjectView(R.id.image_scan)
    ImageView imageScan;
    @InjectView(R.id.ll_search)
    LinearLayout llSearch;
    @InjectView(R.id.search)
    Button search;
    @InjectView(R.id.orderListview)
    ListView orderListview;
    private int endId=0;
    private Subscriber<TransportBean> subscriber;
    private List<TransportBean.ModelBean.ItemsBean> dataList;
    private PiciLeftAdapter adapter;
    private String id;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_pici_add;
    }

    @Override
    public void initView(View view) {
        EventBus.getDefault().register(this);
        ButterKnife.inject(this, view);
    }

    @Override
    public void initData() {
        dataList = new ArrayList<>();
        adapter = new PiciLeftAdapter(getActivity(), dataList);
        orderListview.setAdapter(adapter);
        id=getActivity().getIntent().getStringExtra("batchid");
        adapter.setBatchid(id);
    }

    private void getTransportInfo(String s, int endId, String userid) {
        showWaitingDialog("数据加载中……");
        subscriber = new Subscriber<TransportBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                showToast(e.toString());
            }

            @Override
            public void onNext(TransportBean transportBean) {
                if (transportBean.getCode().equals("200")) {
                    TransportBean.ModelBean model = transportBean.getModel();
                    if (model.getItems().size() > 0) {
                        dataList = model.getItems();
                        adapter.refresh(dataList);
                    } else {
                        showToast("暂无数据");
                    }
                } else {
                    showToast("获取数据失败");
                }
            }
        };
        HttpUtils.getInstance().getTransportInfo(subscriber, s, endId + "", userid);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
        EventBus.getDefault().unregister(this);
    }


    /**
     * 使用onEventMainThread来接收事件，那么不论分发事件在哪个线程运行，接收事件永远在UI线程执行
     * 可以用来更新UI
     *
     * @param event
     */
    public void onEventMainThread(EndEvent event) {
        endId = event.getId();
        nextStation.setText(event.getName());
    }

    @OnClick({R.id.next_station, R.id.image_scan, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next_station:
                startActivity(new Intent(getActivity(), SearchEndActivity.class));
                break;
            case R.id.image_scan:
                break;
            case R.id.search:
                if(endId==0){
                    showToast("请选择下一站地址");
                    return;
                }
                String search= CommonUtils.getTextViewValue(etHdbhSearch);
                getTransportInfo(search,endId,AppConstant.userid);
                break;
        }
    }

}
